package com.wbao.dianniu.expandtabview;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.ChildZone;
import com.wbao.dianniu.data.Zone;
import com.wbao.dianniu.utils.JsonUtil;
import com.wbao.dianniu.utils.Utils;

public class ViewLeft extends LinearLayout implements ViewBaseAction {

	private ListView regionListView;
	private ListView plateListView;
	private ArrayList<String> groups = new ArrayList<String>();
	private LinkedList<String> childrenItem = new LinkedList<String>();
	private SparseArray<LinkedList<String>> children = new SparseArray<LinkedList<String>>();
	//	第一列
	private TextAdapter earaListViewAdapter;
	// 第二列
	private TextAdapter plateListViewAdapter;
	private OnSelectListener mOnSelectListener;
//	第一列位置
	private int tEaraPosition = 0;
	private int tBlockPosition = 0;
	private String showString = "地区";

	public ViewLeft(Context context) {
		super(context);
		init(context);
	}

	public ViewLeft(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

//	public void updateShowText(String showArea, String showBlock) {
//		if (showArea == null || showBlock == null) {
//			return;
//		}
//		for (int i = 0; i < groups.size(); i++) {
//			if (groups.get(i).equals(showArea)) {
//				earaListViewAdapter.setSelectedPosition(i);
//				childrenItem.clear();
//				if (i < children.size()) {
//					childrenItem.addAll(children.get(i));
//				}
//				tEaraPosition = i;
//				break;
//			}
//		}
//		for (int j = 0; j < childrenItem.size(); j++) {
//			if (childrenItem.get(j).replace("全部", "").equals(showBlock.trim())) {
//				plateListViewAdapter.setSelectedPosition(j);
//				tBlockPosition = j;
//				break;
//			}
//		}
//		setDefaultSelect();
//	}

	private void init(Context context) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_region, this, true);
		regionListView = (ListView) findViewById(R.id.listView);
		plateListView = (ListView) findViewById(R.id.listView2);
		setBackgroundDrawable(getResources().getDrawable(R.drawable.choosearea_bg_right));

		InputStream input = getResources().openRawResource(R.raw.zone2);
		String jsonData = Utils.inputStream2String(input, "utf-8");
		List<Zone> zoneList = (List<Zone>) JsonUtil.toObjectList(jsonData, Zone.class);
		for (int i = 0; i < zoneList.size(); i++) {
			groups.add( zoneList.get(i).getName());
			LinkedList<ChildZone> linkList = zoneList.get(i).getChildrens();
			LinkedList<String> arrayList = new LinkedList<String>();
			for (int j = 0; j < linkList.size(); j++) {
				arrayList.add(linkList.get(j).getName());
			}
			children.put(i, arrayList);
		}
		
		earaListViewAdapter = new TextAdapter(context, groups,
				R.drawable.choose_item_selected,
				R.drawable.choose_eara_item_selector);
		earaListViewAdapter.setTextSize(14);
		earaListViewAdapter.setSelectedPositionNoNotify(tEaraPosition);
		regionListView.setAdapter(earaListViewAdapter);
		earaListViewAdapter
				.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(View view, int position) {
						tEaraPosition = position;
						if (position < children.size()) {
							childrenItem.clear();
							childrenItem.addAll(children.get(position));
							plateListViewAdapter.notifyDataSetChanged();
						}
					}
				});
		if (tEaraPosition < children.size())
			childrenItem.addAll(children.get(tEaraPosition));
		plateListViewAdapter = new TextAdapter(context, childrenItem,
				R.drawable.choose_item_right,
				R.drawable.choose_plate_item_selector);
		plateListViewAdapter.setTextSize(14);
		plateListViewAdapter.setSelectedPositionNoNotify(tBlockPosition);
		plateListView.setAdapter(plateListViewAdapter);
		plateListViewAdapter
				.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(View view, final int position) {
						showString = groups.get(tEaraPosition) + childrenItem.get(position);
						if (showString.contains("全部")) {
							showString = groups.get(tEaraPosition);
						}
						if (mOnSelectListener != null) {
							mOnSelectListener.getValue(showString);
						}

					}
				});
		if (tBlockPosition < childrenItem.size())
			showString = childrenItem.get(tBlockPosition);
		if (showString.contains("全部")) {
			showString = showString.replace("全部", "地区");
		}
		setDefaultSelect();

	}

	public void setDefaultSelect() {
		regionListView.setSelection(tEaraPosition);
		plateListView.setSelection(tBlockPosition);
	}

	public String getShowText() {
		return showString;
	}

	public void setOnSelectListener(OnSelectListener onSelectListener) {
		mOnSelectListener = onSelectListener;
	}

	public interface OnSelectListener {
		public void getValue(String showText);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}
}
